package com.sql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConMysql {
	
	public Connection con = null;
	public Statement sql = null;
	public ResultSet rs = null;
	
	String connectionUrl = "jdbc:mysql://localhost:3306/test";
	String dbName = "root";
	String dbPassword = "123456";
	
	public void connMysql() throws ClassNotFoundException {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(connectionUrl, dbName, dbPassword);
			sql = con.createStatement();
			rs = sql.executeQuery("select * from user");
			if(con != null) {
				System.out.println("数据库连接成功！");
				while(rs.next()) {
					System.out.println(rs.getString(1).toString());
					System.out.println(rs.getString(2).toString());
					System.out.println(rs.getString(3).toString());
					System.out.println(rs.getString(4).toString());
				}
			} else {
				System.out.println("数据库连接失败！");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	

	public static void main(String[] args) throws ClassNotFoundException {
		ConMysql con = new ConMysql();
		con.connMysql();
	}

}
